package tools.beans;

import entities.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.EntityManagerAux;
/**
 *
 * @author jgineste
 */
public class VerificationAJAX extends HttpServlet {
    @EJB
    private EntityManagerAux em;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String idUser = request.getParameter("idUser");
        String Destinataire = request.getParameter("Destinataire");
        String reponse = "";
        System.out.println("idUser avant if : " + idUser);
        reponse = valid(true,"Votre login est disponible");
        boolean u;
        
        try{
        if (!idUser.isEmpty() && !idUser.equals(null)) {
            System.out.println("entree if");
            u= em.exists(idUser);

            if (!u) {
                reponse = valid(true,"Votre login est disponible");
            } else {
                reponse = valid(false,"Votre login est déjà utilisé !");
            }
        }
        }
        catch(Exception e){

        if (!Destinataire.isEmpty() && !Destinataire.equals(null)) {
            System.out.println("entree if");
            u = em.exists(Destinataire);

            if (!u) {
                reponse = valid(false,"Cet user n'existe pas !");
            } else {
                reponse = valid(true,"Cet user existe.");
            }
        }
        }
        
        request.setAttribute("verifajax", reponse);
        request.getRequestDispatcher("ajax.jsp").forward(request, response);
    }

    String valid(boolean valid,String message)
	{
        return "<img src=\"" + (valid ? "./images/valid.gif" : "./images/invalid.gif") + "\" border=\"0\"/> <i>" + message + "</i>";
	}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}